package com.tinklabs.demo.mvp.model;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;

import com.tinklabs.demo.bean.CityGuideResult;
import com.tinklabs.demo.net.CallbackListener;
import com.tinklabs.demo.utils.CollectionUtils;
import com.tinklabs.demo.utils.FileUtils;

/**
 * Created by Administrator on 2018/1/19.
 */

public class CityGuideModel {
    // get local data
    public void getData(Context context, CallbackListener<List<CityGuideResult.Item>> listener) {
        // get local json
        String jsonStr = new FileUtils().readJSONFromAsset(context, "city_guide.json");

        List<CityGuideResult.Item> dataList = parseJsonData(jsonStr);

        if (CollectionUtils.isEmpty(dataList)) {
            listener.onError("获取数据失败，请重试");
        } else {
            listener.onSuccess(dataList);
        }
    }

    /**
     * @param json
     * @return parse Json
     */
    public List<CityGuideResult.Item> parseJsonData(String json) {
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(CityGuideResult.class, new CityGuideResult.TypeModelDeserializer())
                .create();

        CityGuideResult result = gson.fromJson(json, CityGuideResult.class);
        return result.getDataList();
    }
}
